﻿
@{
    ViewBag.Title = "隆基报表";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
    var workshops = ViewBag.Workshops as List<IOA.MES.DataAccess.VModel.VWorkshop>;
    var startTime = DateTime.Now.TimeOfDay < new TimeSpan(8, 0, 0) ? DateTime.Today.AddDays(-1).AddHours(20) : (DateTime.Now.TimeOfDay < new TimeSpan(20, 0, 0) ? DateTime.Today.AddHours(8) : DateTime.Today.AddHours(20));
    var endTime = DateTime.Now.TimeOfDay < new TimeSpan(8, 0, 0) ? DateTime.Today.AddHours(8) : (DateTime.Now.TimeOfDay < new TimeSpan(20, 0, 0) ? DateTime.Today.AddHours(20) : DateTime.Today.AddDays(1).AddHours(8));
}
@section Contents{
    <style>
        .layui-form-label {
            width: auto;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.WorkShop">车间：</label>
                        <div class="layui-input-inline">
                            <select id="selWorkshops" lay-filter="selWorkshops">
                                @foreach (var item in workshops)
                                {
                                    <option value="@item.WorkshopCode" @(workshops.IndexOf(item) == 0 ? " selected " : "")>@item.WorkshopName</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.AssemblyLine">流水线：</label>
                        <div class="layui-input-inline">
                            <select id="selLines" lay-filter="selLines">
                                <option value='0'>全部</option>
                                @for (var stackNo = 1; stackNo <= 12; stackNo++)
                                {
                                    <option value="@stackNo">@stackNo</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.StackNo">堆号：</label>
                        <div class="layui-input-inline">
                            <select id="selStackNo" lay-filter="selStackNo">
                                <option value="0">全部</option>
                                @for (var stackNo = 1; stackNo <= 12; stackNo++)
                                {
                                    <option value="@stackNo">@stackNo</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.CreateTime">时间：</label>
                        <div class="layui-input-inline" style="width: 310px;">
                            <input type="text" id="txtRangeTime" value="@(startTime.ToString("yyyy-MM-dd HH:mm:ss") + " 至 " + endTime.ToString("yyyy-MM-dd HH:mm:ss"))" autocomplete="off" class="layui-input rangetime">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn layuiadmin-btn-list" type="button">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                        <a id="btnDownload" class="layui-btn layui-btn-normal layuiadmin-btn-list" href=":;" target="_blank">
                            <i class="layui-icon layui-icon-download-circle layuiadmin-button-btn"></i>
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-card">
            <div class="layui-card-body">
                <table id="table" lay-filter="table"></table>
            </div>
        </div>
    </div>
</script>

@section Scripts{
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });
        setTimeout(function () {
            initLayui();
        }, 50);

        function initLayui() {
            layui.use(['table', 'layer', 'form', 'laydate', 'element'], function () {
                var layer = layui.layer,
                    table = layui.table,
                    form = layui.form,
                    laydate = layui.laydate;
                laydate.render({
                    elem: '#txtRangeTime'
                    , type: 'datetime'
                    , range: '至'
                });
                form.on('select(selWorkshops)', function (data) {
                    console.log(data.value); //得到被选中的值
                    queryLines();
                });

                queryLines();
                function queryLines() {
                    var loadId = layui.layer.load();
                    $.ajax({
                        url: "/Report/QueryLines",
                        data: {
                            workshopCode: $("#selWorkshops").val().trim()
                        },
                        complete: function () {
                            layui.layer.close(loadId);
                        },
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layui.layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }
                            var html = ["<option value='0'>全部</option>"];
                            for (var i = 0; i < res.Data.length; i++) {
                                html.push("<option value='" + res.Data[i].PkId + "'>" + res.Data[i].LineName + "</option>");
                            }
                            $("#selLines").html(html.join(""));
                            form.render("select");
                        }
                    });
                }

                var options = {
                    elem: '#table'
                    , url: '/Report/QuerySortStacks'
                    , method: "post"
                    , where: { workshopCode: "", lineId: 0, stackNo: 0, startTime: null, endTime: null }
                    , page: {
                        limit: 15
                        , limits: [15, 20, 25, 30, 40, 50]
                    }
                    , response: {
                        statusName: 'ErrCode'
                        , statusCode: 200
                        , msgName: 'ErrCode'
                        , countName: 'TotalCount'
                        , dataName: 'Data'
                    }
                    , cols: [[
                        { title: '序号', type: 'numbers', width: 80 }
                        , { field: 'WorkshopName', title: '车间', minWidth: 100 }
                        , { field: 'LineName', title: '流水线', minWidth: 100 }
                        , { field: 'MachineName', title: '设备', minWidth: 100 }
                        , { field: 'StackNo', title: '堆号', minWidth: 100 }
                        , { field: 'FullCount', title: '满堆数量', minWidth: 100 }
                        , { field: 'SortNo', title: '总块数', minWidth: 100 }
                        , { field: 'CNStatus', title: '状态', minWidth: 100 }
                        , { field: 'CreateTimeStr', title: '创建时间', minWidth: 100 }
                    ]]
                };

                $("#btnSearch").click(function () {
                    queryTable();
                });
                queryTable();
                function queryTable() {
                    options.page.curr = 1;
                    var rangeTime = $("#txtRangeTime").val().trim();
                    var startTime = rangeTime == "" ? null : rangeTime.split('至')[0].trim(),
                        endTime = rangeTime == "" ? null : rangeTime.split('至')[1].trim();
                    options.where.workshopCode = $("#selWorkshops").val().trim();
                    options.where.lineId = parseInt($("#selLines").val().trim());
                    options.where.stackNo = parseInt($("#selStackNo").val().trim());
                    options.where.startTime = startTime;
                    options.where.endTime = endTime;

                    if (table.index <= 0) {
                        table.render(options);
                        table.on('rowDouble(table)', function (obj) {
                            parent.layui.index.openTabsPage("/Report/SortStackRecord?stackId=" + obj.data.PkId, "整堆组件(" + obj.data.PkId + ")");
                        });
                    } else {
                        table.reload("table", options);
                    }
                }
                $("#btnDownload").click(function () {
                    if (options.where.workshopCode.trim() == '' || options.where.startTime == null) {
                        layer.msg("导出报表必须选择车间和过站时间", { icon: 5 });
                        return false;
                    }

                    var paras = new Array();
                    for (var item in options.where) {
                        paras.push(item + "=" + options.where[item]);
                    }
                    $(this).attr("href", "/Report/ExporSortStacks?" + paras.join("&"));
                });
            });
        }
    </script>
}
